/*
 * Copyright (c) 2007-2008, Petr Panteleyev
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *    3. The name of the author may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.panteleyev.gclient;

import org.panteleyev.blogclient.BloggerEditorFrame;
import org.panteleyev.html.Editor;
import org.panteleyev.html.PlainEditor;
import org.panteleyev.swing.BasicDialog;

/**
 *
 * @author  Petr Panteleyev
 */
public class GBCommentEditFrame extends BloggerEditorFrame<GBUserProfile> {
    private GBAccount               _account;
    private GBEntry                 _entry;
    private PlainEditor             editor = new PlainEditor();

    public GBCommentEditFrame(GBUserProfile profile, GBEntry entry) {
        super(profile);
        
        _account = getProfile().getAccount();
        _entry = entry;
        
        initComponents();
        createMenuBar();
        
        editor.setEditor(_textEdit);
        editor.newDocument();
        
        
        StringBuilder b = new StringBuilder(L10N.COMMENTS_EDIT_FRAME_TITLE.s());
        b.append("\"");
        b.append(getPost().getSubject());
        b.append("\" ");
        setTitle(b.toString());
        
        /* Setup undo/redo */
        updateUndoRedo(false, false);
        getTextUndoManager().addComponent(_textEdit);
    }

    private void createMenuBar() {
        createMenuBar(
            createFileMenu(),
            createProfileMenu(),
            createToolsMenu(),
            createWindowMenu(),
            createHelpMenu()
        );
    }
        
    public GBEntry getPost() { return _entry; }
    
    @Override
    public void insertText(String text) {
        _textEdit.replaceSelection(text);
    }
    
    @Override
    public String getSelectedText() {
        return editor.getSelectedText();
    }
    
    @Override
    public Editor getEditor() {
        return editor;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jScrollPane2 = new javax.swing.JScrollPane();
        _textEdit = new javax.swing.JTextPane();
        _buttonPanel = new javax.swing.JPanel();
        _okButton = new javax.swing.JButton();
        _cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jScrollPane2.setViewportView(_textEdit);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        getContentPane().add(jScrollPane2, gridBagConstraints);

        _okButton.setText("OK");
        _okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _okButtonActionPerformed(evt);
            }
        });
        _buttonPanel.add(_okButton);

        _cancelButton.setText(BasicDialog.CANCEL);
        _cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _cancelButtonActionPerformed(evt);
            }
        });
        _buttonPanel.add(_cancelButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 1.0;
        getContentPane().add(_buttonPanel, gridBagConstraints);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-746)/2, (screenSize.height-515)/2, 746, 515);
    }// </editor-fold>//GEN-END:initComponents

private void _okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__okButtonActionPerformed
        String text = _textEdit.getText();
        if (text.length() != 0) {
            GBComment comment = new GBComment(getPost());
            comment.setBody(text);
            getProfile().addNewComment(comment);
            dispose();
        }
}//GEN-LAST:event__okButtonActionPerformed

private void _cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__cancelButtonActionPerformed
         dispose();
}//GEN-LAST:event__cancelButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel _buttonPanel;
    private javax.swing.JButton _cancelButton;
    private javax.swing.JButton _okButton;
    private javax.swing.JTextPane _textEdit;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables

}
